//
//  XPTabBar.m
//  SinaWeibo
//
//  Created by 徐攀 on 16/5/11.
//  Copyright © 2016年 徐攀. All rights reserved.
//

#import "XPTabBar.h"


@interface XPTabBar ()

@property (nonatomic, strong) UIButton *composeButton;

@end

@implementation XPTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.composeButton];
    }
    return self;
}

#pragma mark - 懒加载
- (UIButton *)composeButton {
    if (!_composeButton) {
        _composeButton = [[UIButton alloc] init];
        
        [_composeButton addTarget:self
                           action:@selector(composeBtnClick)
                 forControlEvents:UIControlEventTouchUpInside];
        
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [_composeButton sizeToFit];
    }
    return _composeButton;
}

#pragma mark - 撰写按钮的点击事件
- (void)composeBtnClick {
    if (self.callBack != nil) {
        self.callBack();
    }
}

#pragma mark - 控件布局
- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.composeButton.center = CGPointMake(self.size_w / 2, self.size_h / 2);
    
    __block NSInteger index = 0;
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull view, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            view.origin_x = index * self.size_w / 5;
            
            index++;
            if (index == 2) {
                index++;
            }
        }
    }];
    
}

@end
